import 'package:dio/dio.dart';

class DioUtil {
  static final DioUtil _instance = DioUtil._internal();

  factory DioUtil() => _instance;

  late final Dio _dio;

  DioUtil._internal() {
    _dio = Dio(BaseOptions(
      baseUrl: "https://api.example.com", // 替换为实际的API base URL
      connectTimeout: const Duration(seconds: 10),
      receiveTimeout: const Duration(seconds: 15),
      headers: {
        'Content-Type': 'application/json',
      },
    ));

    // 添加请求、响应、错误的拦截器
    _dio.interceptors.add(InterceptorsWrapper(
      onRequest: (options, handler) {
        print('Request: ${options.uri}');
        return handler.next(options);
      },
      onResponse: (response, handler) {
        print('Response: ${response.statusCode}');
        return handler.next(response);
      },
      onError: (DioError error, handler) {
        print('Error: ${error.message}');
        return handler.next(error);
      },
    ));
  }

  // 暴露 dio 实例
  Dio get dio => _dio;

  // GET 请求
  Future<Response?> get(String path,
      {Map<String, dynamic>? queryParameters, Options? options}) async {
    try {
      Response response = await _dio.get(path,
          queryParameters: queryParameters, options: options);
      return response;
    } on DioError catch (e) {
      return _handleError(e);
    }
  }

  // POST 请求
  Future<Response?> post(String path,
      {dynamic data,
      Map<String, dynamic>? queryParameters,
      Options? options}) async {
    try {
      Response response = await _dio.post(path,
          data: data, queryParameters: queryParameters, options: options);
      return response;
    } on DioError catch (e) {
      return _handleError(e);
    }
  }

  // PUT 请求
  Future<Response?> put(String path,
      {dynamic data,
      Map<String, dynamic>? queryParameters,
      Options? options}) async {
    try {
      Response response = await _dio.put(path,
          data: data, queryParameters: queryParameters, options: options);
      return response;
    } on DioError catch (e) {
      return _handleError(e);
    }
  }

  // DELETE 请求
  Future<Response?> delete(String path,
      {dynamic data,
      Map<String, dynamic>? queryParameters,
      Options? options}) async {
    try {
      Response response = await _dio.delete(path,
          data: data, queryParameters: queryParameters, options: options);
      return response;
    } on DioError catch (e) {
      return _handleError(e);
    }
  }

  // 错误处理
  Response? _handleError(DioError error) {
    switch (error.type) {
      case DioErrorType.connectionTimeout:
        print('Connection Timeout');
        break;
      case DioErrorType.sendTimeout:
        print('Send Timeout');
        break;
      case DioErrorType.receiveTimeout:
        print('Receive Timeout');
        break;
      case DioErrorType.badResponse:
        print('Bad Response: ${error.response?.statusCode}');
        break;
      case DioErrorType.cancel:
        print('Request Cancelled');
        break;
      case DioErrorType.unknown:
        print('Unknown Error: ${error.message}');
        break;
      default:
        print('Error: ${error.message}');
        break;
    }
    return error.response;
  }
}
